home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / tests / regexp.test < prev    next >
Encoding:
Text File  |  1997-08-15  |  10.9 KB  |  319 lines  |  [TEXT/ALFA]

  1. # Commands covered:  regexp, regsub
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # Copyright (c) 1994 Sun Microsystems, Inc.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. # SCCS: @(#) regexp.test 1.21 96/12/23 13:59:48
  14.  
  15. if {[string compare test [info procs test]] == 1} then {source defs}
  16.  
  17. catch {unset foo}
  18. test regexp-1.1 {basic regexp operation} {
  19.     regexp ab*c abbbc
  20. } 1
  21. test regexp-1.2 {basic regexp operation} {
  22.     regexp ab*c ac
  23. } 1
  24. test regexp-1.3 {basic regexp operation} {
  25.     regexp ab*c ab
  26. } 0
  27. test regexp-1.4 {basic regexp operation} {
  28.     regexp -- -gorp abc-gorpxxx
  29. } 1
  30. test regexp-1.5 {basic regexp operation} {
  31.     regexp {^([^ ]*)[ ]*([^ ]*)} "" a
  32. } 1
  33.  
  34. test regexp-2.1 {getting substrings back from regexp} {
  35.     set foo {}
  36.     list [regexp ab*c abbbbc foo] $foo
  37. } {1 abbbbc}
  38. test regexp-2.2 {getting substrings back from regexp} {
  39.     set foo {}
  40.     set f2 {}
  41.     list [regexp a(b*)c abbbbc foo f2] $foo $f2
  42. } {1 abbbbc bbbb}
  43. test regexp-2.3 {getting substrings back from regexp} {
  44.     set foo {}
  45.     set f2 {}
  46.     list [regexp a(b*)(c) abbbbc foo f2] $foo $f2
  47. } {1 abbbbc bbbb}
  48. test regexp-2.4 {getting substrings back from regexp} {
  49.     set foo {}
  50.     set f2 {}
  51.     set f3 {}
  52.     list [regexp a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
  53. } {1 abbbbc bbbb c}
  54. test regexp-2.5 {getting substrings back from regexp} {
  55.     set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
  56.     set f6 {}; set f7 {}; set f8 {}; set f9 {}; set fa {}; set fb {};
  57.     list [regexp (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*)(a*)(b*) \
  58.           12223345556789999aabbb \
  59.         foo f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb] $foo $f1 $f2 $f3 $f4 $f5 \
  60.         $f6 $f7 $f8 $f9 $fa $fb
  61. } {1 12223345556789999aabbb 1 222 33 4 555 6 7 8 9999 aa bbb}
  62. test regexp-2.6 {getting substrings back from regexp} {
  63.     set foo 2; set f2 2; set f3 2; set f4 2
  64.     list [regexp (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
  65. } {1 a a {} {}}
  66. test regexp-2.7 {getting substrings back from regexp} {
  67.     set foo 1; set f2 1; set f3 1; set f4 1
  68.     list [regexp (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
  69. } {1 ac a {} c}
  70.  
  71.  
  72. test regexp-3.1 {-indices option to regexp} {
  73.     set foo {}
  74.     list [regexp -indices ab*c abbbbc foo] $foo
  75. } {1 {0 5}}
  76. test regexp-3.2 {-indices option to regexp} {
  77.     set foo {}
  78.     set f2 {}
  79.     list [regexp -indices a(b*)c abbbbc foo f2] $foo $f2
  80. } {1 {0 5} {1 4}}
  81. test regexp-3.3 {-indices option to regexp} {
  82.     set foo {}
  83.     set f2 {}
  84.     list [regexp -indices a(b*)(c) abbbbc foo f2] $foo $f2
  85. } {1 {0 5} {1 4}}
  86. test regexp-3.4 {-indices option to regexp} {
  87.     set foo {}
  88.     set f2 {}
  89.     set f3 {}
  90.     list [regexp -indices a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
  91. } {1 {0 5} {1 4} {5 5}}
  92. test regexp-3.5 {-indices option to regexp} {
  93.     set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
  94.     set f6 {}; set f7 {}; set f8 {}; set f9 {}
  95.     list [regexp -indices (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*) \
  96.         12223345556789999 \
  97.         foo f1 f2 f3 f4 f5 f6 f7 f8 f9] $foo $f1 $f2 $f3 $f4 $f5 \
  98.         $f6 $f7 $f8 $f9
  99. } {1 {0 16} {0 0} {1 3} {4 5} {6 6} {7 9} {10 10} {11 11} {12 12} {13 16}}
  100. test regexp-3.6 {getting substrings back from regexp} {
  101.     set foo 2; set f2 2; set f3 2; set f4 2
  102.     list [regexp -indices (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
  103. } {1 {1 1} {1 1} {-1 -1} {-1 -1}}
  104. test regexp-3.7 {getting substrings back from regexp} {
  105.     set foo 1; set f2 1; set f3 1; set f4 1
  106.     list [regexp -indices (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
  107. } {1 {1 2} {1 1} {-1 -1} {2 2}}
  108.  
  109. test regexp-4.1 {-nocase option to regexp} {
  110.     regexp -nocase foo abcFOo
  111. } 1
  112. test regexp-4.2 {-nocase option to regexp} {
  113.     set f1 22
  114.     set f2 33
  115.     set f3 44
  116.     list [regexp -nocase {a(b*)([xy]*)z} aBbbxYXxxZ22 f1 f2 f3] $f1 $f2 $f3
  117. } {1 aBbbxYXxxZ Bbb xYXxx}
  118. test regexp-4.3 {-nocase option to regexp} {
  119.     regexp -nocase FOo abcFOo
  120. } 1
  121. set x abcdefghijklmnopqrstuvwxyz1234567890
  122. set x $x$x$x$x$x$x$x$x$x$x$x$x
  123. test regexp-4.4 {case conversion in regsub} {
  124.     list [regexp -nocase $x $x foo] $foo
  125. } "1 $x"
  126. unset x
  127.  
  128. test regexp-5.1 {exercise cache of compiled expressions} {
  129.     regexp .*a b
  130.     regexp .*b c
  131.     regexp .*c d
  132.     regexp .*d e
  133.     regexp .*e f
  134.     regexp .*a bbba
  135. } 1
  136. test regexp-5.2 {exercise cache of compiled expressions} {
  137.     regexp .*a b
  138.     regexp .*b c
  139.     regexp .*c d
  140.     regexp .*d e
  141.     regexp .*e f
  142.     regexp .*b xxxb
  143. } 1
  144. test regexp-5.3 {exercise cache of compiled expressions} {
  145.     regexp .*a b
  146.     regexp .*b c
  147.     regexp .*c d
  148.     regexp .*d e
  149.     regexp .*e f
  150.     regexp .*c yyyc
  151. } 1
  152. test regexp-5.4 {exercise cache of compiled expressions} {
  153.     regexp .*a b
  154.     regexp .*b c
  155.     regexp .*c d
  156.     regexp .*d e
  157.     regexp .*e f
  158.     regexp .*d 1d
  159. } 1
  160. test regexp-5.5 {exercise cache of compiled expressions} {
  161.     regexp .*a b
  162.     regexp .*b c
  163.     regexp .*c d
  164.     regexp .*d e
  165.     regexp .*e f
  166.     regexp .*e xe
  167. } 1
  168.  
  169. test regexp-6.1 {regexp errors} {
  170.     list [catch {regexp a} msg] $msg
  171. } {1 {wrong # args: should be "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"}}
  172. test regexp-6.2 {regexp errors} {
  173.     list [catch {regexp -nocase a} msg] $msg
  174. } {1 {wrong # args: should be "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"}}
  175. test regexp-6.3 {regexp errors} {
  176.     list [catch {regexp -gorp a} msg] $msg
  177. } {1 {bad switch "-gorp": must be -indices, -nocase, or --}}
  178. test regexp-6.4 {regexp errors} {
  179.     list [catch {regexp a( b} msg] $msg
  180. } {1 {couldn't compile regular expression pattern: unmatched ()}}
  181. test regexp-6.5 {regexp errors} {
  182.     list [catch {regexp a( b} msg] $msg
  183. } {1 {couldn't compile regular expression pattern: unmatched ()}}
  184. test regexp-6.6 {regexp errors} {
  185.     list [catch {regexp a a f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1} msg] $msg
  186. } {0 1}
  187. test regexp-6.7 {regexp errors} {
  188.     list [catch {regexp (x)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.) xyzzy} msg] $msg
  189. } {1 {couldn't compile regular expression pattern: too many ()}}
  190. test regexp-6.8 {regexp errors} {
  191.     set f1 44
  192.     list [catch {regexp abc abc f1(f2)} msg] $msg
  193. } {1 {couldn't set variable "f1(f2)"}}
  194.  
  195. test regexp-7.1 {basic regsub operation} {
  196.     list [regsub aa+ xaxaaaxaa 111&222 foo] $foo
  197. } {1 xax111aaa222xaa}
  198. test regexp-7.2 {basic regsub operation} {
  199.     list [regsub aa+ aaaxaa &111 foo] $foo
  200. } {1 aaa111xaa}
  201. test regexp-7.3 {basic regsub operation} {
  202.     list [regsub aa+ xaxaaa 111& foo] $foo
  203. } {1 xax111aaa}
  204. test regexp-7.4 {basic regsub operation} {
  205.     list [regsub aa+ aaa 11&2&333 foo] $foo
  206. } {1 11aaa2aaa333}
  207. test regexp-7.5 {basic regsub operation} {
  208.     list [regsub aa+ xaxaaaxaa &2&333 foo] $foo
  209. } {1 xaxaaa2aaa333xaa}
  210. test regexp-7.6 {basic regsub operation} {
  211.     list [regsub aa+ xaxaaaxaa 1&22& foo] $foo
  212. } {1 xax1aaa22aaaxaa}
  213. test regexp-7.7 {basic regsub operation} {
  214.     list [regsub a(a+) xaxaaaxaa {1\122\1} foo] $foo
  215. } {1 xax1aa22aaxaa}
  216. test regexp-7.8 {basic regsub operation} {
  217.     list [regsub a(a+) xaxaaaxaa {1\\\122\1} foo] $foo
  218. } "1 {xax1\\aa22aaxaa}"
  219. test regexp-7.9 {basic regsub operation} {
  220.     list [regsub a(a+) xaxaaaxaa {1\\122\1} foo] $foo
  221. } "1 {xax1\\122aaxaa}"
  222. test regexp-7.10 {basic regsub operation} {
  223.     list [regsub a(a+) xaxaaaxaa {1\\&\1} foo] $foo
  224. } "1 {xax1\\aaaaaxaa}"
  225. test regexp-7.11 {basic regsub operation} {
  226.     list [regsub a(a+) xaxaaaxaa {1\&\1} foo] $foo
  227. } {1 xax1&aaxaa}
  228. test regexp-7.12 {basic regsub operation} {
  229.     list [regsub a(a+) xaxaaaxaa {\1\1\1\1&&} foo] $foo
  230. } {1 xaxaaaaaaaaaaaaaaxaa}
  231. test regexp-7.13 {basic regsub operation} {
  232.     set foo xxx
  233.     list [regsub abc xyz 111 foo] $foo
  234. } {0 xyz}
  235. test regexp-7.14 {basic regsub operation} {
  236.     set foo xxx
  237.     list [regsub ^ xyz "111 " foo] $foo
  238. } {1 {111 xyz}}
  239. test regexp-7.15 {basic regsub operation} {
  240.     set foo xxx
  241.     list [regsub -- -foo abc-foodef "111 " foo] $foo
  242. } {1 {abc111 def}}
  243. test regexp-7.16 {basic regsub operation} {
  244.     set foo xxx
  245.     list [regsub x "" y foo] $foo
  246. } {0 {}}
  247.  
  248. test regexp-8.1 {case conversion in regsub} {
  249.     list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
  250. } {1 xaAAaAAay}
  251. test regexp-8.2 {case conversion in regsub} {
  252.     list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
  253. } {1 xaAAaAAay}
  254. test regexp-8.3 {case conversion in regsub} {
  255.     set foo 123
  256.     list [regsub a(a+) xaAAaAAay & foo] $foo
  257. } {0 xaAAaAAay}
  258. test regexp-8.4 {case conversion in regsub} {
  259.     set foo 123
  260.     list [regsub -nocase a CaDE b foo] $foo
  261. } {1 CbDE}
  262. test regexp-8.5 {case conversion in regsub} {
  263.     set foo 123
  264.     list [regsub -nocase XYZ CxYzD b foo] $foo
  265. } {1 CbD}
  266. test regexp-8.6 {case conversion in regsub} {
  267.     set x abcdefghijklmnopqrstuvwxyz1234567890
  268.     set x $x$x$x$x$x$x$x$x$x$x$x$x
  269.     set foo 123
  270.     list [regsub -nocase $x $x b foo] $foo
  271. } {1 b}
  272.  
  273. test regexp-9.1 {-all option to regsub} {
  274.     set foo 86
  275.     list [regsub -all x+ axxxbxxcxdx |&| foo] $foo
  276. } {4 a|xxx|b|xx|c|x|d|x|}
  277. test regexp-9.2 {-all option to regsub} {
  278.     set foo 86
  279.     list [regsub -nocase -all x+ aXxXbxxcXdx |&| foo] $foo
  280. } {4 a|XxX|b|xx|c|X|d|x|}
  281. test regexp-9.3 {-all option to regsub} {
  282.     set foo 86
  283.     list [regsub x+ axxxbxxcxdx |&| foo] $foo
  284. } {1 a|xxx|bxxcxdx}
  285. test regexp-9.4 {-all option to regsub} {
  286.     set foo 86
  287.     list [regsub -all bc axxxbxxcxdx |&| foo] $foo
  288. } {0 axxxbxxcxdx}
  289. test regexp-9.5 {-all option to regsub} {
  290.     set foo xxx
  291.     list [regsub -all node "node node more" yy foo] $foo
  292. } {2 {yy yy more}}
  293. test regexp-9.6 {-all option to regsub} {
  294.     set foo xxx
  295.     list [regsub -all ^ xxx 123 foo] $foo
  296. } {1 123xxx}
  297.  
  298. test regexp-10.1 {regsub errors} {
  299.     list [catch {regsub a b c} msg] $msg
  300. } {1 {wrong # args: should be "regsub ?switches? exp string subSpec varName"}}
  301. test regexp-10.2 {regsub errors} {
  302.     list [catch {regsub -nocase a b c} msg] $msg
  303. } {1 {wrong # args: should be "regsub ?switches? exp string subSpec varName"}}
  304. test regexp-10.3 {regsub errors} {
  305.     list [catch {regsub -nocase -all a b c} msg] $msg
  306. } {1 {wrong # args: should be "regsub ?switches? exp string subSpec varName"}}
  307. test regexp-10.4 {regsub errors} {
  308.     list [catch {regsub a b c d e f} msg] $msg
  309. } {1 {wrong # args: should be "regsub ?switches? exp string subSpec varName"}}
  310. test regexp-10.5 {regsub errors} {
  311.     list [catch {regsub -gorp a b c} msg] $msg
  312. } {1 {bad switch "-gorp": must be -all, -nocase, or --}}
  313. test regexp-10.6 {regsub errors} {
  314.     list [catch {regsub -nocase a( b c d} msg] $msg
  315. } {1 {couldn't compile regular expression pattern: unmatched ()}}
  316. test regexp-10.7 {regsub errors} {
  317.     list [catch {regsub -nocase aaa aaa xxx f1(f2)} msg] $msg
  318. } {1 {couldn't set variable "f1(f2)"}}
  319.